using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
public partial class Homnay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        tbSanPhamCollection spCollection = tbSanPhamBUS.GetAll();
        repeater1.DataSource = spCollection;
        repeater1.DataBind();
    }

    protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            tbSanPham sanPham = (tbSanPham)e.Item.DataItem;
            HyperLink hyp_tensp = (HyperLink)e.Item.FindControl("hyp_tensp");
            Literal lit_old = (Literal)e.Item.FindControl("lit_old");
            Literal lit_giaban = (Literal)e.Item.FindControl("lit_giaban");
            Literal lit_tietkiem = (Literal)e.Item.FindControl("lit_tietkiem");
            Literal lit_count = (Literal)e.Item.FindControl("lit_count");
            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
            hyp_tensp.Text = sanPham.TenSP;
            lit_old.Text = sanPham.GiaTriThuc.ToString ();
            lit_giaban.Text = sanPham.GiaBan.ToString ();
            lit_count.Text = sanPham.SLDaBan.ToString();
            int tietkiem= sanPham .GiaTriThuc - sanPham .GiaBan ;
            lit_tietkiem.Text = tietkiem.ToString();

            int Id = 0;
            int MaHA = tbHinhAnhBUS.FirstImage(int.Parse(sanPham.IdSanPham.ToString()), Id);
            if (MaHA > 0)
            {
                tbHinhAnh hinhAnh = tbHinhAnhBUS.GetById(MaHA);
                img_HinhAnh.ImageUrl = "~/Photos/" + hinhAnh.TenHinh.ToString();
            }
            else
            {
                img_HinhAnh.ImageUrl = "~/Photos/no_images.jpg";
            }
        }
    }
}
